﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using Microsoft.Practices.Unity;
using Sern.Abstracts.Tracker.DataContracts;
using Sern.Common;

namespace Sern.ServiceImplementation
{
    public class LocalRegistrar : IComponentRegistrar
    {
        public void Register(IUnityContainer container)
        {
            container.RegisterType<IClientTypeToEnumMapper, ClientTypeToEnumMapper>();
            container.RegisterType<IListenerFactory, ListenerFactory>();

            //Registration for the service host listing.            

            //Register the binding factory for the different client type.
            container.RegisterInstance<IBindingFactory>(new BindingFactory() {
                {
                    ClientType.Collector,
                    () =>
                        new BindingScheme{
                            ClientType = ClientType.Collector,
                            Binding = 
                                new NetHttpBinding(BasicHttpSecurityMode.None, false)
                                    {
                                        MaxBufferPoolSize = int.MaxValue / 8,
                                        MaxReceivedMessageSize = int.MaxValue / 8,
                                        MaxBufferSize = int.MaxValue / 8,
                                        ReceiveTimeout = new TimeSpan(0, 2, 0, 0),
                                        SendTimeout = new TimeSpan(0,2,0,0),
                                        OpenTimeout = new TimeSpan(0, 2, 0, 0),
                                        ReaderQuotas = XmlDictionaryReaderQuotas.Max,
                                        CloseTimeout = new TimeSpan(0, 2, 0, 0),                                                        
                                    },
                            Scheme = "http",
                            Port = 8080
                        }
                },
                {
                    ClientType.Feeder,
                    () => new BindingScheme
                        {
                            ClientType = ClientType.Feeder,
                            Binding= 
                                new NetHttpBinding(BasicHttpSecurityMode.None, false)
                                    {
                                        MaxBufferPoolSize = int.MaxValue / 8,
                                        MaxReceivedMessageSize = int.MaxValue / 8,
                                        MaxBufferSize = int.MaxValue / 8,
                                        ReceiveTimeout = new TimeSpan(0, 2, 0, 0),
                                        SendTimeout = new TimeSpan(0,2,0,0),
                                        OpenTimeout = new TimeSpan(0, 2, 0, 0),
                                        ReaderQuotas = XmlDictionaryReaderQuotas.Max,
                                        CloseTimeout = new TimeSpan(0, 2, 0, 0),                                                        
                                    },
                            Scheme = "http",
                            Port = 8080
                        }                    
                },
                {
                    ClientType.Tracker,
                    () => new BindingScheme
                        {
                            ClientType = ClientType.Tracker,
                            Binding= 
                                new NetHttpBinding(BasicHttpSecurityMode.None, false)
                                    {
                                        MaxBufferPoolSize = int.MaxValue / 8,
                                        MaxReceivedMessageSize = int.MaxValue / 8,
                                        MaxBufferSize = int.MaxValue / 8,
                                        ReceiveTimeout = new TimeSpan(0, 2, 0, 0),
                                        SendTimeout = new TimeSpan(0,2,0,0),
                                        OpenTimeout = new TimeSpan(0, 2, 0, 0),
                                        ReaderQuotas = XmlDictionaryReaderQuotas.Max,
                                        CloseTimeout = new TimeSpan(0, 2, 0, 0),                                                        
                                    },
                            Scheme = "http",
                            Port = 8080
                        }                    
                },
                {
                    ClientType.TrackerHost,
                    () => new BindingScheme
                        {
                            ClientType = ClientType.TrackerHost,
                            Binding= 
                                new NetHttpBinding(BasicHttpSecurityMode.None, false)
                                    {
                                        MaxBufferPoolSize = int.MaxValue / 8,
                                        MaxReceivedMessageSize = int.MaxValue / 8,
                                        MaxBufferSize = int.MaxValue / 8,
                                        ReceiveTimeout = new TimeSpan(0, 2, 0, 0),
                                        SendTimeout = new TimeSpan(0,2,0,0),
                                        OpenTimeout = new TimeSpan(0, 2, 0, 0),
                                        ReaderQuotas = XmlDictionaryReaderQuotas.Max,
                                        CloseTimeout = new TimeSpan(0, 2, 0, 0),                                                        
                                    },
                            Scheme = "http",
                            Port = 8080
                        }                    
                },
                {
                    ClientType.MapReduceHost,
                    () => new BindingScheme
                        {
                            ClientType = ClientType.MapReduceHost,
                            Binding= 
                                new NetHttpBinding(BasicHttpSecurityMode.None, false)
                                    {
                                        MaxBufferPoolSize = int.MaxValue / 8,
                                        MaxReceivedMessageSize = int.MaxValue / 8,
                                        MaxBufferSize = int.MaxValue / 8,
                                        ReceiveTimeout = new TimeSpan(0, 2, 0, 0),
                                        SendTimeout = new TimeSpan(0,2,0,0),
                                        OpenTimeout = new TimeSpan(0, 2, 0, 0),
                                        ReaderQuotas = XmlDictionaryReaderQuotas.Max,
                                        CloseTimeout = new TimeSpan(0, 2, 0, 0),                                                        
                                    },
                            Scheme = "http",
                            Port = 8080
                        }                    
                },
                {
                    ClientType.Filter,
                    () => 
                        new BindingScheme{
                            ClientType = ClientType.Filter,
                            Binding = 
                                new NetMsmqBinding()
                                    {
                                        DeadLetterQueue = DeadLetterQueue.System,
                                        Durable = true,
                                        ExactlyOnce = true,
                                        MaxRetryCycles = 10,
                                        UseMsmqTracing = false,
                                        ReceiveErrorHandling = ReceiveErrorHandling.Move,
                                        MaxBufferPoolSize = int.MaxValue / 8,
                                        MaxReceivedMessageSize = int.MaxValue / 8,
                                        ReceiveTimeout = new TimeSpan(0, 2, 0, 0),
                                        SendTimeout = new TimeSpan(0,2,0,0),
                                        OpenTimeout = new TimeSpan(0, 2, 0, 0),
                                        ReaderQuotas = XmlDictionaryReaderQuotas.Max,
                                        CloseTimeout = new TimeSpan(0, 2, 0, 0),
                                        Security = new NetMsmqSecurity
                                            {
                                                Mode = NetMsmqSecurityMode.None
                                            },
                                        QueueTransferProtocol = QueueTransferProtocol.Srmp
                                    },
                            Scheme = "net.msmq",
                            PathPrefix = "private"
                        }
                },
                {
                    ClientType.Booster,
                    () => 
                        new BindingScheme{
                            ClientType = ClientType.Booster,
                            Binding = 
                                new NetMsmqBinding()
                                    {
                                        DeadLetterQueue = DeadLetterQueue.System,
                                        Durable = true,
                                        ExactlyOnce = true,
                                        MaxRetryCycles = 10,
                                        UseMsmqTracing = false,
                                        ReceiveErrorHandling = ReceiveErrorHandling.Move,
                                        MaxBufferPoolSize = int.MaxValue / 8,
                                        MaxReceivedMessageSize = int.MaxValue / 8,
                                        ReceiveTimeout = new TimeSpan(0, 2, 0, 0),
                                        SendTimeout = new TimeSpan(0,2,0,0),
                                        OpenTimeout = new TimeSpan(0, 2, 0, 0),
                                        ReaderQuotas = XmlDictionaryReaderQuotas.Max,
                                        CloseTimeout = new TimeSpan(0, 2, 0, 0),
                                        Security = new NetMsmqSecurity
                                            {
                                                Mode = NetMsmqSecurityMode.None
                                            },
                                        QueueTransferProtocol = QueueTransferProtocol.Srmp
                                    },
                            Scheme = "net.msmq",
                            PathPrefix = "private"
                        }                    
                },
                {
                    ClientType.Splitter,
                    () => 
                        new BindingScheme{
                            ClientType = ClientType.Splitter,
                            Binding = 
                                new NetMsmqBinding()
                                    {
                                        DeadLetterQueue = DeadLetterQueue.System,
                                        Durable = true,
                                        ExactlyOnce = true,
                                        MaxRetryCycles = 10,
                                        UseMsmqTracing = false,
                                        ReceiveErrorHandling = ReceiveErrorHandling.Move,
                                        MaxBufferPoolSize = int.MaxValue / 8,
                                        MaxReceivedMessageSize = int.MaxValue / 8,
                                        ReceiveTimeout = new TimeSpan(0, 2, 0, 0),
                                        SendTimeout = new TimeSpan(0,2,0,0),
                                        OpenTimeout = new TimeSpan(0, 2, 0, 0),
                                        ReaderQuotas = XmlDictionaryReaderQuotas.Max,
                                        CloseTimeout = new TimeSpan(0, 2, 0, 0),
                                        Security = new NetMsmqSecurity
                                            {
                                                Mode = NetMsmqSecurityMode.None
                                            },
                                        QueueTransferProtocol = QueueTransferProtocol.Srmp
                                    },
                            Scheme = "net.msmq",
                            PathPrefix = "private"
                        }                
                    },
                { 
                    ClientType.TrackerMonitor,
                    () => 
                        new BindingScheme{
                            ClientType = ClientType.TrackerMonitor,
                            Binding = 
                                new NetMsmqBinding()
                                    {
                                        DeadLetterQueue = DeadLetterQueue.System,
                                        Durable = true,
                                        ExactlyOnce = true,
                                        MaxRetryCycles = 10,
                                        UseMsmqTracing = false,
                                        ReceiveErrorHandling = ReceiveErrorHandling.Move,
                                        MaxBufferPoolSize = int.MaxValue / 8,
                                        MaxReceivedMessageSize = int.MaxValue / 8,
                                        ReceiveTimeout = new TimeSpan(0, 2, 0, 0),
                                        SendTimeout = new TimeSpan(0,2,0,0),
                                        OpenTimeout = new TimeSpan(0, 2, 0, 0),
                                        ReaderQuotas = XmlDictionaryReaderQuotas.Max,
                                        CloseTimeout = new TimeSpan(0, 2, 0, 0),
                                        Security = new NetMsmqSecurity
                                            {
                                                Mode = NetMsmqSecurityMode.None
                                            },
                                        QueueTransferProtocol = QueueTransferProtocol.Srmp
                                    },
                            Scheme = "net.msmq",
                            PathPrefix = "private"

                        }                },
                { 
                    ClientType.TrackerLogging,
                    () => 
                        new BindingScheme{
                            ClientType = ClientType.TrackerLogging,
                            Binding = 
                                new NetMsmqBinding()
                                    {
                                        DeadLetterQueue = DeadLetterQueue.System,
                                        Durable = true,
                                        ExactlyOnce = true,
                                        MaxRetryCycles = 10,
                                        UseMsmqTracing = false,
                                        ReceiveErrorHandling = ReceiveErrorHandling.Move,
                                        MaxBufferPoolSize = int.MaxValue / 8,
                                        MaxReceivedMessageSize = int.MaxValue / 8,
                                        ReceiveTimeout = new TimeSpan(0, 2, 0, 0),
                                        SendTimeout = new TimeSpan(0,2,0,0),
                                        OpenTimeout = new TimeSpan(0, 2, 0, 0),
                                        ReaderQuotas = XmlDictionaryReaderQuotas.Max,
                                        CloseTimeout = new TimeSpan(0, 2, 0, 0),
                                        Security = new NetMsmqSecurity
                                            {
                                                Mode = NetMsmqSecurityMode.None
                                            },
                                        QueueTransferProtocol = QueueTransferProtocol.Srmp
                            
                                    },
                            Scheme = "net.msmq",
                            PathPrefix = "private"
                        }                    
                },
                { 
                    ClientType.TrackerTelemetry,
                    () => 
                        new BindingScheme{
                            ClientType = ClientType.TrackerTelemetry,
                            Binding = 
                                new NetMsmqBinding()
                                    {
                                        DeadLetterQueue = DeadLetterQueue.System,
                                        Durable = true,
                                        ExactlyOnce = true,
                                        MaxRetryCycles = 10,
                                        UseMsmqTracing = false,
                                        ReceiveErrorHandling = ReceiveErrorHandling.Move,
                                        MaxBufferPoolSize = int.MaxValue / 8,
                                        MaxReceivedMessageSize = int.MaxValue / 8,
                                        ReceiveTimeout = new TimeSpan(0, 2, 0, 0),
                                        SendTimeout = new TimeSpan(0,2,0,0),
                                        OpenTimeout = new TimeSpan(0, 2, 0, 0),
                                        ReaderQuotas = XmlDictionaryReaderQuotas.Max,
                                        CloseTimeout = new TimeSpan(0, 2, 0, 0),
                                        Security = new NetMsmqSecurity
                                            {
                                                Mode = NetMsmqSecurityMode.None
                                            },
                                        QueueTransferProtocol = QueueTransferProtocol.Srmp
                                    },
                            Scheme = "net.msmq",
                            PathPrefix = "private"
                        }
                }                
            });
        }
    }
}
